﻿using BasePlatformNet;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BasePlatformCommon;
using System.Threading;

namespace L9SubSet
{
    public partial class Form_ArtNetSet : Form
    {
        private int portNum = 8;
        private int devID = 1;

        BaseCoreNet baseCoreNet;

        private string section = "ArtNetSet";									// 多语言文件配置节名称

        public Form_ArtNetSet(BaseCoreNet netCore)
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterParent;
            baseCoreNet = netCore;
            initInviorment();
        }

        private void initInviorment()
        {
            targetPosTextBox.Text = "1";
            PortNumComboBox.SelectedIndex = 0;
            ip1TextBox.Text = "192";
            ip2TextBox.Text = "168";
            ip3TextBox.Text = "1";
            ip4TextBox.Text = "129";
            cbAuto.Checked = true;
        }

        private void PortNumComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (PortNumComboBox.SelectedIndex == 0)
            {
                portNum = 8;
            }
            else
            {
                portNum = 16;
            }
            initUnvView();
        }

        private void initUnvView()
        {
            if (PortUnvView.Rows.Count == portNum)
            {
                for (int i = 0; i < portNum; i++)
                {
                    PortUnvView.Rows[i].Cells[1].Value = String.Format("{0}", i);
                }
                return;
            }
            PortUnvView.Rows.Clear();
            for (int i = 0; i < portNum; i++)
            {
                PortUnvView.Rows.Add();
                PortUnvView.Rows[i].Cells[0].Value = (i + 1).ToString();
                PortUnvView.Rows[i].Cells[1].Value = String.Format("{0}", i);
            }
        }

        private void targetPosTextBox_Leave(object sender, EventArgs e)
        {
            int id = -1;
            int.TryParse(targetPosTextBox.Text, out id);
            if (id <= 0 || id >= 256)
            {
                var msg = "序号需要在[1,255]之间";
                if (LanguageOper.available)
                {
                    msg = LanguageOper.ReadValue(section, "msgStr.0");
                }
                Tools.ShowTipMessage(this, msg);
                targetPosTextBox.Focus();
                return;
            }
            else
            {
                int ip4 = -1;
                int.TryParse(ip4TextBox.Text, out ip4);
                if (ip4 < 0 || ip4 > 255 || ip4 == (128 + devID))
                {
                    ip4TextBox.Text = (128 + id).ToString();
                }
                devID = id;
                initUnvView();
            }
        }

        private void cbAuto_CheckedChanged(object sender, EventArgs e) {
            if (cbAuto.Checked) {
                pnlIP.Enabled = false;
            } else {
                pnlIP.Enabled = true;
            }
        }

        private void applyButton_Click(object sender, EventArgs e)
        {
            // 检查各项参数
            byte[] data = new byte[24];
            int ip1, ip2, ip3, ip4;
            int.TryParse(ip1TextBox.Text, out ip1);
            int.TryParse(ip2TextBox.Text, out ip2);
            int.TryParse(ip3TextBox.Text, out ip3);
            int.TryParse(ip4TextBox.Text, out ip4);
            if (ip1 < 0 || ip1 > 255 || ip2 < 0 || ip2 > 255 || ip3 < 0 || ip3 > 255 || ip4 < 0 || ip4 > 255)
            {
                var msg = "IP地址设置错误";
                if (LanguageOper.available)
                {
                    msg = LanguageOper.ReadValue(section, "msgStr.1");
                }
                Tools.ShowTipMessage(this, msg);
                return;
            }

            data[0] = 0xab;
            data[1] = 0;
            data[2] = 0;
            data[3] = (byte)(cbAuto.Checked ? 1 : 0);
            
            data[4] = (byte)ip1;
            data[5] = (byte)ip2;
            data[6] = (byte)ip3;
            data[7] = (byte)ip4;
            for (int i = 0; i < 16; i++)
            {
                data[8 + i] = (byte)i;
            }
            int activeNetDevIndex = baseCoreNet.getActiveDevIndex();
            if (activeNetDevIndex == -1)
            {
                var msg = "当前未连接分控";
                if (LanguageOper.available)
                {
                    msg = LanguageOper.ReadValue(section, "msgStr.2");
                }
                Tools.ShowTipMessage(this, msg);
                return;
            }

            baseCoreNet.write2Flash(activeNetDevIndex, devID, data, 0x20100000, 0x20000000, 24, true);
            Thread.Sleep(10);

            var len = 2 + 2 * portNum;
            data = new byte[len];
            data[0] = 0xab;
            data[1] = (byte)portNum;
            var index = 2;
            for (var i = 0; i < portNum; i++) {
                var unvStr = PortUnvView.Rows[i].Cells[1].Value.ToString();
                var unv = 0;
                if (int.TryParse(unvStr, out unv)) {
                    var arr = Tools.int2Byte2SmallEnd(unv);
                    Tools.addByte(ref data, arr, ref index);
                }
            }
            baseCoreNet.write2Flash(activeNetDevIndex, devID, data, 0x20100400, 0x20000400, len, true);

            DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void exitButton_Click(object sender, EventArgs e)
        {
            DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        public void setLanguageText()
        {
            this.Text = LanguageOper.ReadValue(section, "ArtNetSet.Text");
            PortCol.HeaderText = LanguageOper.ReadValue(section, "PortCol.Text");
            targetSubLabel.Text = LanguageOper.ReadValue(section, "targetSubLabel.Text");
            lblPortNum.Text = LanguageOper.ReadValue(section, "lblPortNum.Text");
            ipLabe1.Text = LanguageOper.ReadValue(section, "ipLabe1.Text");
            processLabel.Text = LanguageOper.ReadValue(section, "processLabel.Text");
            applyButton.Text = LanguageOper.ReadValue(section, "applyButton.Text");
            exitButton.Text = LanguageOper.ReadValue(section, "exitButton.Text");
            cbAuto.Text = LanguageOper.ReadValue(section, "cbAuto.Text");
        }
    }
}
